package cn.edu.xmu.oomall.customer.service;

import cn.edu.xmu.oomall.core.util.JacksonUtil;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.dao.NewCustomerDao;
import cn.edu.xmu.oomall.customer.model.bo.Customer;
import cn.edu.xmu.oomall.customer.model.po.CustomerPo;
import cn.edu.xmu.oomall.customer.model.vo.NewCustomerVo;
import cn.edu.xmu.oomall.customer.model.vo.SimpleCustomerRetVo;
import cn.edu.xmu.privilegegateway.annotation.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
/**
 * @author Jiazhe Yuan
 * @date 2021-12-14 12:43:37
 */
public class NewCustomerService {

    @Autowired
    private NewCustomerDao newCustomerDao;

    @Transactional(rollbackFor = Exception.class)
    public ReturnObject registerCustomerAccount(NewCustomerVo newCustomerVo) {
        Customer customer = Common.cloneVo(newCustomerVo, Customer.class);
        customer.setName(newCustomerVo.getName());
        customer.setBeDeleted((byte) 0);
        customer.setCreatorName(newCustomerVo.getUserName());
        customer.setCreatorId(1L);
        customer.setPoint(0L);
        customer.setState((byte) 0);
        ReturnObject ret = newCustomerDao.createNewCustomerByBo(customer);
        if(ret.getData() == null) {
            return ret;
        }
        CustomerPo customerPo = (CustomerPo) ret.getData();
        SimpleCustomerRetVo simpleCustomerRetVo = Common.cloneVo(customerPo, SimpleCustomerRetVo.class);
        simpleCustomerRetVo.setName(customerPo.getName());
        return new ReturnObject(simpleCustomerRetVo);
    }
}
